package com.kavenka.zhufengfm.app.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

/**
 * Created by kavenka on 2015/9/27 0027.
 */
public class DiscoverCategoryProvider extends ContentProvider {

    public static final UriMatcher matcher;

    public static final int CODE_DISCOVER_CATEGORY = 1;

    static {
        matcher=new UriMatcher(0);
        matcher.addURI(
                DatabaseContract.BASE_AUTHORITY,
                DatabaseContract.TABLE_DISCOVER_CATEGORIES,
                CODE_DISCOVER_CATEGORY
        );
    }

    private DBHelper helper;

    @Override
    public boolean onCreate() {
        helper = new DBHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Cursor cursor=null;
        if (uri != null) {
            int code=matcher.match(uri);
            SQLiteDatabase db = helper.getReadableDatabase();
            switch (code){
                case CODE_DISCOVER_CATEGORY:
                    db.query(DatabaseContract.TABLE_DISCOVER_CATEGORIES,
                            projection,
                            selection,
                            selectionArgs,
                            null,
                            null,
                            sortOrder);
                    break;
            }
        }
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        Uri ret=null;
        if (uri != null&&values!=null) {
            int code=matcher.match(uri);
            SQLiteDatabase db = helper.getWritableDatabase();
            long rid=-1;
            switch (code){
                case CODE_DISCOVER_CATEGORY:
                    rid = db.insert(
                            DatabaseContract.TABLE_DISCOVER_CATEGORIES,
                            null,
                            values
                    );
                    break;
            }
            db.close();
            if (rid!=-1){
                ret= ContentUris.withAppendedId(uri,rid);
            }
        }
        return ret;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }
}
